package com.ychen.ruijee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ychen.ruijee.bean.Category;
import com.ychen.ruijee.bean.Dish;
import com.ychen.ruijee.bean.DishFlavor;
import com.ychen.ruijee.dao.DishDao;
import com.ychen.ruijee.dto.DishDto;
import com.ychen.ruijee.service.CategoryService;
import com.ychen.ruijee.service.DishFlavorService;
import com.ychen.ruijee.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ychen@gotop.cn
 * @date 2024-06-02 22:00
 */
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Resource
    private DishFlavorService dishFlavorService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 保存菜品信息
     * 新增 菜品 同时 保存口味信息
     * @param dishDto
     * @return
     */
    @Transactional
    @Override
    public boolean saveDish2Flavor(DishDto dishDto) {
        log.info("要保存的菜品信息是 {}",dishDto.toString());
        // 先保存 菜品 信息
        boolean dishFlag = this.save(dishDto);
        // 获取 菜品 id
        Long disdId = dishDto.getId();
        // 获取 菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 绑定当前的 菜品 id 和 菜品口味
        flavors = flavors.stream().map((item)->{
            item.setDishId(disdId);
            return item;
        }).collect(Collectors.toList());

        // 保存菜品口味 到 口味表中
        boolean dishFlavoeFlag = dishFlavorService.saveBatch(flavors);
        // 菜品新增后 清除 redis 中的 菜品数据
        String keys = "dish_"+dishDto.getCategoryId()+"_1";
        redisTemplate.delete(keys);

        return dishFlag && dishFlavoeFlag ;
    }

    /**
     * 分页查询扎展示 菜品 dish的信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> dishPageInfo(Integer page, Integer pageSize, String name) {
        Page<Dish> dishInfo = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //先查 dish
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.like(name != null,Dish::getName,name);
        dishWrapper.orderByDesc(Dish::getUpdateTime);
        this.page(dishInfo,dishWrapper);

        log.info("菜品dish的分页查询,{}",dishInfo.getRecords());
        // 再查 DishDto 对象
        BeanUtils.copyProperties(dishInfo,dishDtoPage,"records");
        List<Dish> records = dishInfo.getRecords();
        List<DishDto> list = records.stream().map((item)->{
            // 将dish分页结果 中属性 赋值给 dishDto中属性
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            // 根据 id 查询分类对象 获得菜品分类
            Category category = categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        log.info("菜品dishdto的分页查询,{}",dishDtoPage.getRecords());
        return dishDtoPage;


    }

    /**
     * 菜品修改 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getDishDto(Long id) {
        // 根据 id 查询 菜品 dish
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        // 从 dish_flavors 表中查询 菜品口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(wrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 修改菜品数据信息
     * 不管口味新增怎么修改 修改的怎么样 都遵循一个原则 先修改再删除
     * @param dishDto
     * @return
     */
    @Override
    @Transactional
    public boolean saveUpdateDish(DishDto dishDto) {
        log.info("要修改的菜品数据信息为 ：{}",dishDto,toString());
        // 保存数据信息到 dish 表中去
        boolean dishUpdateFlag = this.updateById(dishDto);
        Long dishId = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishId);
        // 因为口味信息不重要 先直接删除 口味信息 再新增
        dishFlavorService.remove(queryWrapper);
        // insert 口味信息到 口味表中去
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item)->{
            // 不仅口味要保存到 dish_flavor 表中 dishID 也要保存到 dish_flavor 表中
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        boolean dishFlavorUpdateFlag = dishFlavorService.saveBatch(flavors);
        // 菜品新增后 清除 redis 中的 菜品数据
        String keys = "dish_"+dishDto.getCategoryId()+"_1";
        redisTemplate.delete(keys);

        return dishUpdateFlag && dishUpdateFlag;
    }
}
